//
//  SXMainTabBar.swift
//  sina
//
//  Created by shengxinwei on 16/5/19.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit

class SXMainTabBar: UITabBar {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
         addSubview(pushBtn)
    }
    
    //从xib中加载视图 会调用这个构造方法
    //在swift中如果重写了构造方法 系统就会默认这个类的对象只能够通过手写代码创建
    //如果从xib中创建 就会报错
    required init?(coder aDecoder: NSCoder) {
        //如果不希望从xib中加载的话 就默认在这个地方崩溃
        fatalError("init(coder:) has not been implemented")
        //        super.init(coder: aDecoder)
    }


    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //按钮宽高
        let w = self.bounds.size.width / 5
        let h = self.bounds.size.height
        var x: CGFloat = 0
        let y: CGFloat = 0
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subView.frame = CGRect(x: w * x, y: y, width: w, height: h)
                x += (x == 1 ? 2 : 1)
            }
        }
        
       
        pushBtn.frame = CGRectOffset(rect, w * 2 , 0)
        bringSubviewToFront(pushBtn)
    
        
    }
    
    //MARK: 懒加载
    lazy  var pushBtn: UIButton = {
        let btn = UIButton()
        //设置btn属性
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        btn.layer.cornerRadius = 5
        btn.layer.masksToBounds = true
        return btn
        
    }()
    
    

}
